package com.example.cursor_controller;

import com.example.cursor_service.UserLogService;
import com.example.cursor_orm.model.UserLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user-logs")
@Tag(name = "用户日志", description = "用户日志相关的API接口")
public class UserLogController {
    private final UserLogService logService;
    
    @Autowired
    public UserLogController(UserLogService logService) {
        this.logService = logService;
    }
    
    @PostMapping
    @Operation(summary = "创建日志", description = "创建新的用户日志")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "日志创建成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public UserLog createLog(@RequestBody UserLog log) { 
        return logService.createLog(log); 
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取日志", description = "根据日志ID获取日志信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取日志成功"),
        @ApiResponse(responseCode = "404", description = "日志不存在")
    })
    public UserLog getLogById(@Parameter(description = "日志ID") @PathVariable Integer id) { 
        return logService.getLogById(id); 
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除日志", description = "根据日志ID删除日志")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "日志删除成功"),
        @ApiResponse(responseCode = "404", description = "日志不存在")
    })
    public boolean deleteLog(@Parameter(description = "日志ID") @PathVariable Integer id) { 
        return logService.deleteLog(id); 
    }
    
    @GetMapping
    @Operation(summary = "获取日志列表", description = "分页获取日志列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取日志列表成功")
    })
    public List<UserLog> getLogList(@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page, 
                                   @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") int size) { 
        return logService.getLogList(page, size); 
    }
    
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户日志", description = "根据用户ID获取该用户的所有日志")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取用户日志成功")
    })
    public List<UserLog> getLogsByUserId(@Parameter(description = "用户ID") @PathVariable Integer userId) { 
        return logService.getLogsByUserId(userId); 
    }
    
    @GetMapping("/action/{action}")
    @Operation(summary = "根据操作类型获取日志", description = "根据操作类型获取相关日志")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取日志成功")
    })
    public List<UserLog> getLogsByAction(@Parameter(description = "操作类型") @PathVariable String action) { 
        return logService.getLogsByAction(action); 
    }
    
    @GetMapping("/time-range")
    @Operation(summary = "根据时间范围获取日志", description = "根据时间范围获取日志")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取日志成功")
    })
    public List<UserLog> getLogsByTimeRange(@Parameter(description = "开始时间") @RequestParam LocalDateTime start, 
                                           @Parameter(description = "结束时间") @RequestParam LocalDateTime end) { 
        return logService.getLogsByTimeRange(start, end); 
    }
    
    @DeleteMapping("/before")
    @Operation(summary = "删除指定时间之前的日志", description = "删除指定时间之前的所有日志")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "日志删除成功")
    })
    public int deleteLogsBefore(@Parameter(description = "截止时间") @RequestParam LocalDateTime beforeTime) { 
        return logService.deleteLogsBefore(beforeTime); 
    }
} 